﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnlamp.DBUtility;//Please add references
namespace jnlamp.DAL
{
	/// <summary>
	/// 数据访问类:standardInformation
	/// </summary>
	public partial class standardInformation
	{
		public standardInformation()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("s_id", "standardInformation"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int s_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from standardInformation");
			strSql.Append(" where s_id=@s_id");
			SqlParameter[] parameters = {
					new SqlParameter("@s_id", SqlDbType.Int,4)
			};
			parameters[0].Value = s_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnlamp.Model.standardInformation model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into standardInformation(");
			strSql.Append("standardnumber,treeage,dbh,treeheight,BranchNumber,Crown,aspect,crowndensity,vegetationtype,StandProperties,ForestOwnership,PlantName,area,uid)");
			strSql.Append(" values (");
			strSql.Append("@standardnumber,@treeage,@dbh,@treeheight,@BranchNumber,@Crown,@aspect,@crowndensity,@vegetationtype,@StandProperties,@ForestOwnership,@PlantName,@area,@uid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@standardnumber", SqlDbType.VarChar,20),
					new SqlParameter("@treeage", SqlDbType.VarChar,10),
					new SqlParameter("@dbh", SqlDbType.VarChar,20),
					new SqlParameter("@treeheight", SqlDbType.VarChar,20),
					new SqlParameter("@BranchNumber", SqlDbType.VarChar,20),
					new SqlParameter("@Crown", SqlDbType.VarChar,20),
					new SqlParameter("@aspect", SqlDbType.VarChar,20),
					new SqlParameter("@crowndensity", SqlDbType.VarChar,20),
					new SqlParameter("@vegetationtype", SqlDbType.VarChar,50),
					new SqlParameter("@StandProperties", SqlDbType.VarChar,50),
					new SqlParameter("@ForestOwnership", SqlDbType.VarChar,50),
					new SqlParameter("@PlantName", SqlDbType.VarChar,200),
					new SqlParameter("@area", SqlDbType.VarChar,20),
                    new SqlParameter("@uid", SqlDbType.Int,4)
                                        };
			parameters[0].Value = model.standardnumber;
			parameters[1].Value = model.treeage;
			parameters[2].Value = model.dbh;
			parameters[3].Value = model.treeheight;
			parameters[4].Value = model.BranchNumber;
			parameters[5].Value = model.Crown;
			parameters[6].Value = model.aspect;
			parameters[7].Value = model.crowndensity;
			parameters[8].Value = model.vegetationtype;
			parameters[9].Value = model.StandProperties;
			parameters[10].Value = model.ForestOwnership;
			parameters[11].Value = model.PlantName;
			parameters[12].Value = model.area;
            parameters[13].Value = model.uid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnlamp.Model.standardInformation model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update standardInformation set ");
			strSql.Append("standardnumber=@standardnumber,");
			strSql.Append("treeage=@treeage,");
			strSql.Append("dbh=@dbh,");
			strSql.Append("treeheight=@treeheight,");
			strSql.Append("BranchNumber=@BranchNumber,");
			strSql.Append("Crown=@Crown,");
			strSql.Append("aspect=@aspect,");
			strSql.Append("crowndensity=@crowndensity,");
			strSql.Append("vegetationtype=@vegetationtype,");
			strSql.Append("StandProperties=@StandProperties,");
			strSql.Append("ForestOwnership=@ForestOwnership,");
			strSql.Append("PlantName=@PlantName,");
			strSql.Append("area=@area");
			strSql.Append(" where s_id=@s_id");
			SqlParameter[] parameters = {
					new SqlParameter("@standardnumber", SqlDbType.VarChar,20),
					new SqlParameter("@treeage", SqlDbType.VarChar,10),
					new SqlParameter("@dbh", SqlDbType.VarChar,20),
					new SqlParameter("@treeheight", SqlDbType.VarChar,20),
					new SqlParameter("@BranchNumber", SqlDbType.VarChar,20),
					new SqlParameter("@Crown", SqlDbType.VarChar,20),
					new SqlParameter("@aspect", SqlDbType.VarChar,20),
					new SqlParameter("@crowndensity", SqlDbType.VarChar,20),
					new SqlParameter("@vegetationtype", SqlDbType.VarChar,50),
					new SqlParameter("@StandProperties", SqlDbType.VarChar,50),
					new SqlParameter("@ForestOwnership", SqlDbType.VarChar,50),
					new SqlParameter("@PlantName", SqlDbType.VarChar,200),
					new SqlParameter("@area", SqlDbType.VarChar,20),
					new SqlParameter("@s_id", SqlDbType.Int,4)};
			parameters[0].Value = model.standardnumber;
			parameters[1].Value = model.treeage;
			parameters[2].Value = model.dbh;
			parameters[3].Value = model.treeheight;
			parameters[4].Value = model.BranchNumber;
			parameters[5].Value = model.Crown;
			parameters[6].Value = model.aspect;
			parameters[7].Value = model.crowndensity;
			parameters[8].Value = model.vegetationtype;
			parameters[9].Value = model.StandProperties;
			parameters[10].Value = model.ForestOwnership;
			parameters[11].Value = model.PlantName;
			parameters[12].Value = model.area;
			parameters[13].Value = model.s_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int s_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from standardInformation ");
			strSql.Append(" where s_id=@s_id");
			SqlParameter[] parameters = {
					new SqlParameter("@s_id", SqlDbType.Int,4)
			};
			parameters[0].Value = s_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string s_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from standardInformation ");
			strSql.Append(" where s_id in ("+s_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnlamp.Model.standardInformation GetModel(int s_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 s_id,standardnumber,treeage,dbh,treeheight,BranchNumber,Crown,aspect,crowndensity,vegetationtype,StandProperties,ForestOwnership,PlantName,area from standardInformation ");
			strSql.Append(" where s_id=@s_id");
			SqlParameter[] parameters = {
					new SqlParameter("@s_id", SqlDbType.Int,4)
			};
			parameters[0].Value = s_id;

			jnlamp.Model.standardInformation model=new jnlamp.Model.standardInformation();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnlamp.Model.standardInformation DataRowToModel(DataRow row)
		{
			jnlamp.Model.standardInformation model=new jnlamp.Model.standardInformation();
			if (row != null)
			{
				if(row["s_id"]!=null && row["s_id"].ToString()!="")
				{
					model.s_id=int.Parse(row["s_id"].ToString());
				}
				if(row["standardnumber"]!=null)
				{
					model.standardnumber=row["standardnumber"].ToString();
				}
				if(row["treeage"]!=null)
				{
					model.treeage=row["treeage"].ToString();
				}
				if(row["dbh"]!=null)
				{
					model.dbh=row["dbh"].ToString();
				}
				if(row["treeheight"]!=null)
				{
					model.treeheight=row["treeheight"].ToString();
				}
				if(row["BranchNumber"]!=null)
				{
					model.BranchNumber=row["BranchNumber"].ToString();
				}
				if(row["Crown"]!=null)
				{
					model.Crown=row["Crown"].ToString();
				}
				if(row["aspect"]!=null)
				{
					model.aspect=row["aspect"].ToString();
				}
				if(row["crowndensity"]!=null)
				{
					model.crowndensity=row["crowndensity"].ToString();
				}
				if(row["vegetationtype"]!=null)
				{
					model.vegetationtype=row["vegetationtype"].ToString();
				}
				if(row["StandProperties"]!=null)
				{
					model.StandProperties=row["StandProperties"].ToString();
				}
				if(row["ForestOwnership"]!=null)
				{
					model.ForestOwnership=row["ForestOwnership"].ToString();
				}
				if(row["PlantName"]!=null)
				{
					model.PlantName=row["PlantName"].ToString();
				}
				if(row["area"]!=null)
				{
					model.area=row["area"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select s_id,standardnumber,treeage,dbh,treeheight,BranchNumber,Crown,aspect,crowndensity,vegetationtype,StandProperties,ForestOwnership,PlantName,area ");
			strSql.Append(" FROM standardInformation ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" s_id,standardnumber,treeage,dbh,treeheight,BranchNumber,Crown,aspect,crowndensity,vegetationtype,StandProperties,ForestOwnership,PlantName,area ");
			strSql.Append(" FROM standardInformation ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM standardInformation ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.s_id desc");
			}
			strSql.Append(")AS Row, T.*  from standardInformation T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "standardInformation";
			parameters[1].Value = "s_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

